/*
 * Copyright (c) 2008 Benoit Bouchez, Nicolas Giard
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.noocodecommit.wicket.stuff.jquery.plugin.ui.slider;

import com.noocodecommit.wicket.stuff.jquery.plugin.JQPlugin;

/**
 * 
 * @author Benoit Bouchez
 */
public class JQSliderUtils
{

	/**
	 * Create a JQSliderBuilder instance on which you can fluently change settings
	 * 
	 * @param id
	 *            wicket component id
	 * @param range
	 *            slider range
	 * @param style
	 *            slider style
	 * @param key
	 *            plugin unique id on which component is attached
	 * @return JQSliderBuilder
	 */
	public static JQSliderBuilder create(String id, boolean range, String style, String key)
	{
		return new JQSliderBuilder(new JQSlider(id, range, style, (key == null ? JQSliderPlugin.DEFAULT_KEY : key)));
	}

	/**
	 * Create a JQSliderBuilder instance on which you can fluently change settings
	 * 
	 * @param id
	 *            wicket component id
	 * @param range
	 *            slider range
	 * @param style
	 *            slider style
	 * @return JQSliderBuilder
	 */
	public static JQSliderBuilder create(String id, boolean range, String style)
	{
		return new JQSliderBuilder(new JQSlider(id, range, style, null));
	}

	/**
	 * @author Benoit Bouchez
	 */
	public static final class JQSliderBuilder
	{
		/** JQuery component */
		private JQSlider component;
		/** JQuery default options */
		private JQSliderOptions options;

		/**
		 * Constructor
		 * 
		 * @param component
		 *            widget
		 */
		public JQSliderBuilder(JQSlider component)
		{
			this.component = component;
			this.options = new JQSliderOptions();
		}

		/**
		 * Sets plugin init mode
		 * 
		 * @param initMode
		 *            plugin init mode
		 * @return JQSliderBuilder
		 */
		public JQSliderBuilder initMode(int initMode)
		{
			component.getPlugin().setInitMode(initMode);
			return this;
		}

		/**
		 * Sets plugin selector when initMode is PLUGIN_INIT_MODE_ONCE
		 * 
		 * @param initOnceSelector
		 *            JQuery selector
		 * @return JQSliderBuilder
		 */
		public JQSliderBuilder initOnceSelector(String initOnceSelector)
		{
			component.getPlugin().setInitOnceSelector(initOnceSelector);
			return this;
		}

		/**
		 * Sets the handle option value
		 * 
		 * @param handle
		 *            slider handle
		 * @return JQSliderBuilder
		 */
		public JQSliderBuilder handle(String handle)
		{
			options.setHandle(handle);
			return this;
		}

		/**
		 * Sets the handles option value
		 * 
		 * @param start
		 *            handles start
		 * @param min
		 *            handles min
		 * @param max
		 *            handles max
		 * @param id
		 *            handles id
		 * @return JQSliderBuilder
		 */
		public JQSliderBuilder handles(int start, int min, int max, String id)
		{
			options.setHandles(start, min, max, id);
			return this;
		}

		/**
		 * Sets the range option value
		 * 
		 * @param range
		 *            slider range
		 * @return JQSliderBuilder
		 */
		public JQSliderBuilder range(boolean range)
		{
			options.setRange(range);
			return this;
		}

		/**
		 * Sets the min option value
		 * 
		 * @param min
		 *            slider min
		 * @return JQSliderBuilder
		 */
		public JQSliderBuilder min(int min)
		{
			options.setMin(min);
			return this;
		}

		/**
		 * Sets the max option value
		 * 
		 * @param max
		 *            slider max
		 * @return JQSliderBuilder
		 */
		public JQSliderBuilder max(int max)
		{
			options.setMax(max);
			return this;
		}

		/**
		 * Sets the start value option value
		 * 
		 * @param startValue
		 *            slider start value
		 * @return JQSliderBuilder
		 */
		public JQSliderBuilder startValue(int startValue)
		{
			options.setStartValue(startValue);
			return this;
		}

		/**
		 * Sets the stepping option value
		 * 
		 * @param stepping
		 *            slider stepping
		 * @return JQSliderBuilder
		 */
		public JQSliderBuilder stepping(int stepping)
		{
			options.setStepping(stepping);
			return this;
		}

		/**
		 * Sets the steps option value
		 * 
		 * @param steps
		 *            slider steps
		 * @return JQSliderBuilder
		 */
		public JQSliderBuilder steps(int steps)
		{
			options.setSteps(steps);
			return this;
		}

		/**
		 * Sets the axis option value
		 * 
		 * @param axis
		 *            slider axis
		 * @return JQSliderBuilder
		 */
		public JQSliderBuilder axis(String axis)
		{
			options.setAxis(axis);
			return this;
		}

		/**
		 * Sets the stop function
		 * 
		 * @param stopScript
		 *            slider stop function
		 * @return JQSliderBuilder
		 */
		public JQSliderBuilder stopScript(String stopScript)
		{
			options.setStopScript(stopScript);
			return this;
		}

		/**
		 * Sets the start function
		 * 
		 * @param startScript
		 *            slider start function
		 * @return JQSliderBuilder
		 */
		public JQSliderBuilder startScript(String startScript)
		{
			options.setStartScript(startScript);
			return this;
		}

		/**
		 * Sets the slide function
		 * 
		 * @param slideScript
		 *            slider slide function
		 * @return JQSliderBuilder
		 */
		public JQSliderBuilder slideScript(String slideScript)
		{
			options.setSlideScript(slideScript);
			return this;
		}

		/**
		 * Sets the change function
		 * 
		 * @param changeScript
		 *            slider change function
		 * @return JQSliderBuilder
		 */
		public JQSliderBuilder changeScript(String changeScript)
		{
			options.setChangeScript(changeScript);
			return this;
		}

		/**
		 * @return JQSlider
		 */
		public JQSlider build()
		{
			if (component.getPlugin().getInitMode() == JQPlugin.PLUGIN_INIT_ONCE)
				component.getPlugin().setOptions(options);
			else
				component.setOptions(options);
			return component;
		}
	}
}